package com.itheima.googleplay9.ui.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.itheima.googleplay9.R;
import com.itheima.googleplay9.adapter.HomeAdapter;
import com.itheima.googleplay9.api.CacheLoader;
import com.itheima.googleplay9.api.DataLoader;
import com.itheima.googleplay9.api.HttpLoader;
import com.itheima.googleplay9.api.Url;
import com.itheima.googleplay9.bean.AppInfo;
import com.itheima.googleplay9.bean.Home;
import com.itheima.googleplay9.ui.activity.AppDetailActivity;
import com.itheima.googleplay9.ui.view.LoadingPage;
import com.itheima.googleplay9.util.LogUtil;
import com.itheima.googleplay9.util.Utils;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by lxj on 2016/5/23.
 */
public class HomeFragment extends BaseFragment implements PullToRefreshBase.OnRefreshListener
    ,AdapterView.OnItemClickListener{
    private PullToRefreshListView refreshListView;
    private ListView listView;
    private ArrayList<AppInfo> list = new ArrayList<>();
    private HomeAdapter homeAdapter;

    @Override
    protected View getSuccessView() {
//        listView = (ListView) View.inflate(getActivity(), R.layout.listview,null);
        //1.初始化下拉刷新listview
        refreshListView = (PullToRefreshListView) View.inflate(getActivity(), R.layout.ptr_listview,null);
        //2.设置刷新模式，默认是Mode.Pull_FROM_START
        refreshListView.setMode(PullToRefreshBase.Mode.BOTH);//上拉和下拉都有
        //3.设置刷新监听器
        refreshListView.setOnRefreshListener(this);

        //4.获取其中包裹的listview，
        listView = refreshListView.getRefreshableView();

        //去掉listview默认的状态选择器
        listView.setSelector(android.R.color.transparent);
        //去掉listview的默认的分割线
        listView.setDivider(null);

        //给listview设置adapter
        homeAdapter = new HomeAdapter(list);
        listView.setAdapter(homeAdapter);

        //添加条目点击事件
        listView.setOnItemClickListener(this);

        return refreshListView;
    }

    @Override
    protected Object onLoad() {
        //如果是下拉刷新则清空集合数据
        if(refreshListView.getCurrentMode()== PullToRefreshBase.Mode.PULL_FROM_START){
            list.clear();
        }

        final Home home = DataLoader.getInstance().loadBeanData(Url.Home+list.size(), Home.class);
        if(home!=null){
            Utils.runOnUIThread(new Runnable() {
                @Override
                public void run() {
                    //将列表数据放入list中
                    list.addAll(home.getList());
                    //更新adapter
                    homeAdapter.notifyDataSetChanged();
                    //完成刷新状态
                    refreshListView.onRefreshComplete();
                }
            });
        }
        return home;
    }

    @Override
    public void onRefresh(PullToRefreshBase refreshView) {
//        onLoad();
        loadingPage.loadData();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getActivity(), AppDetailActivity.class);
//        LogUtil.e(this,"position: "+position);
        intent.putExtra("packageName",list.get(position-1).getPackageName());
        startActivity(intent);
    }
}
